package com.ysu.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysu.demo.Local.MyLocal;
import com.ysu.demo.VO.ResultVO;
import com.ysu.demo.annotation.PassToken;
import com.ysu.demo.annotation.UserToken;
import com.ysu.demo.dataobject.Project;
import com.ysu.demo.dto.ProjectDTO;
import com.ysu.demo.enums.ExceptionEnum;
import com.ysu.demo.enums.ProjectState;
import com.ysu.demo.exception.BaseException;
import com.ysu.demo.from.project.*;
import com.ysu.demo.service.IProjectService;
import com.ysu.demo.utlis.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhw
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    IProjectService projectService;

    @PostMapping("/admin/list")
    public ResultVO projectList(@RequestBody ProjectListPagesFrom from){
        IPage<Project> res = projectService.allList(from);

        return ResultUtil.TrueResult(res);
    }

    @PostMapping("/admin/one")
    @PassToken
    public ResultVO projectOne(@RequestBody @Valid ProjectOneFrom from,
                               BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        ProjectDTO res = projectService.one(from.getId());

        return ResultUtil.TrueResult(res);
    }

    @PostMapping("/admin/pass")
    public ResultVO projectPass(@RequestBody @Valid ProjectUrlFrom from,
                               BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        projectService.changeState(ProjectState.WAIT_GOD,ProjectState.LIVE,from.getId(),from.getUrl());

        return ResultUtil.TrueResult();
    }

    @PostMapping("/admin/close")
    public ResultVO projectClose(@RequestBody @Valid ProjectUrlFrom from,
                                BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        projectService.changeState(ProjectState.LIVE,ProjectState.OVER,from.getId(),from.getUrl());

        return ResultUtil.TrueResult();
    }

    @PostMapping("/college/apply")  //学院方立项申请
    @UserToken
    public ResultVO projectApply(@RequestBody @Valid ProjectApplyForm form,
                                 BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        if(!form.getCollegeId().equals(MyLocal.getId())){
            throw new BaseException(ExceptionEnum.NOT_PASS);
        }

        projectService.projectApply(form);

        return ResultUtil.TrueResult();
    }

    @PostMapping("/college/list")   //学院方按照学院id查询
    @UserToken
    public ResultVO collegeList(@RequestBody @Valid CollegeListForm form,
                                BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            throw new BaseException(ExceptionEnum.FAIL_CHECK);
        }

        if(!form.getCollegeId().equals(MyLocal.getId())){
            throw new BaseException(ExceptionEnum.NOT_PASS);
        }

        List<Project> list = projectService.collegeList(form);
        return ResultUtil.TrueResult(list);
    }

}
